package boot.spring.config;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;

/**
 * HttpSessionHandshakeInterceptor抽象类：握手拦截器，在握手先后添加操作
 */
@Component
@Slf4j
public class HttpSessionHandshakeInterceptorConfig extends HttpSessionHandshakeInterceptor {

    /**
     * 握手前
     * @param request the current request
     * @param response the current response
     * @param wsHandler the target WebSocket handler
     * @param attributes the attributes from the HTTP handshake to associate with the WebSocket
     * session; the provided attributes are copied, the original map is not used.
     * @return
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                   WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        log.info(request.getRemoteAddress().toString()+"开始握手");
        log.info(request.getURI().toString() +"开始握手;" +"接口类型:"+request.getMethod() +"; 接口协议:"+request.getURI().getScheme());
        Map<String, String>  headers = request.getHeaders().toSingleValueMap();
        log.info(JSON.toJSONString(headers));
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    /**
     * 握手后
     * @param request the current request
     * @param response the current response
     * @param wsHandler the target WebSocket handler
     * @param ex an exception raised during the handshake, or {@code null} if none
     */
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                               WebSocketHandler wsHandler, @Nullable Exception ex) {
        log.info(request.getRemoteAddress().toString()+"握手结束");

        Map<String, String>  headers = request.getHeaders().toSingleValueMap();
        log.info("执行了afterHandshake之前 request 的 headers :  " + JSON.toJSONString(headers));
        log.info("执行了afterHandshake之前 request 的 scheme :  " + request.getURI().getScheme());

        Map<String, String>  responseHeaders =  response.getHeaders().toSingleValueMap();
        log.info("执行了afterHandshake之前 response 的 Headers :  " +  JSON.toJSONString(responseHeaders));

        super.afterHandshake(request, response, wsHandler, ex);

        log.info("执行了afterHandshake之后： "+request.getRemoteAddress().toString()+"握手结束");
        log.info("执行了afterHandshake之后： " + request.getURI().getScheme());

        Map<String, String>  headers2 = request.getHeaders().toSingleValueMap();
        log.info("执行了afterHandshake之后 request 的 headers :  " + JSON.toJSONString(headers2));

        Map<String, String>  responseHeaders2 =  response.getHeaders().toSingleValueMap();
        log.info("执行了afterHandshake之后 response 的 Headers :  " +  JSON.toJSONString(responseHeaders2));
    }

}
